package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.GoodDao;
import dao.GoodUtil;
import dao.Goods;

/**
 * Servlet implementation class DoSaveGood
 */
@WebServlet(
		name="DoSaveGood",
		urlPatterns= {"/servlet/DoSaveGood"},
		loadOnStartup = 1
		
		)
public class DoSaveGood extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoSaveGood() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		response.setContentType("text/html,charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String id=request.getParameter("id");
		String name=request.getParameter("name");
		double price=Double.parseDouble(request.getParameter("price"));
		String detail=request.getParameter("detail");
		String goodclass=request.getParameter("goodclass");
		int number=Integer.parseInt(request.getParameter("number"));
		String brand=request.getParameter("brand");
		String image=request.getParameter("image");
		Goods goodbean=new Goods();
		goodbean.setId(id);
		goodbean.setName(name);
		goodbean.setPrice(price);
		goodbean.setDetail(detail);
		goodbean.setGoodclass(goodclass);
		goodbean.setNumber(number);
		goodbean.setBrand(brand);
		goodbean.setImage(image);
		GoodDao goodDao=new GoodUtil();
		int n=goodDao.update(goodbean);
		if(n>0) {
			request.getRequestDispatcher("/goodchange.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("/error.jsp").forward(request, response);
		}
	}

}
